<template>
  <div class="app-container">
    <div>
      <el-form>
        <el-row class="rowFormItem">
          <el-col :span="3" style="margin-right: 10px;">
            <el-input v-model="keyword" placeholder="请输入类型名称进行搜索" />
          </el-col>
          <el-col :span="3">
             <el-select v-model="status" placeholder="禁用或启用">
               <el-option v-for="opt in options" :key="opt.id" :value="opt.id" :label="opt.label" />
             </el-select>
          </el-col>
          <!-- 提交按钮 -->
          <el-col :span="5" class="clear">
            <el-form-item style="text-align: center;width: 250px;margin-left: 10px;">
              <el-button type="primary" size="large" @click="handleSubmit()">检索</el-button>
              <el-button type="warning" size="large" style="margin-left: 8px" @click="handleReset()">重置</el-button>
              <el-button type="success" size="large" @click="addmodel()">添加</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="tableClass">
      <el-table :data="formdata" v-loading="loading" :header-cell-style="{background:'#eef1f6',color:'#606266'}" border
        style="width: 100%;">
        <el-table-column align="center" prop="id" label="序号" />
        <el-table-column align="center" prop="name" label="类型名称" />
        <el-table-column align="center" prop="desc" label="类型简介" />
        <el-table-column align="center" prop="sort" label="排序" />
        <!-- <el-table-column align="center" prop="status" label="状态">
          <template slot-scope="scope">
            <el-select v-model="scope.row.status" @change="changAdminStatus(scope.row.id)">
              <el-option v-for="opt in options" :key="opt.id" :value="opt.id" :label="opt.label" />
            </el-select>
          </template>
        </el-table-column> -->
        <el-table-column align="center" prop="status" label="状态">
          <template slot-scope="scope">
            <div v-if="scope.row.status==0"><el-tag size="small" type="danger">禁用</el-tag></div>
            <div v-if="scope.row.status==1"><el-tag size="small">启用</el-tag></div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="created_at" label="创建时间" />
        <el-table-column label="操作" align="center" width="230px">
          <template slot-scope="scope">
            <!-- scope.row 获取整行信息 表示当前单元格 #ff9900ba-->
            <el-button class="btnAppeal" size="small" type="warning" @click="updateMsg(scope.row)">修改</el-button>
            <el-button class="btnAppeal" size="small" type="danger" @click="delMsg(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <br>
      <!-- 分页 -->
      <div style="text-align: center;">
        <el-pagination background layout="prev, pager, next" @current-change="pageChange" :page-size="pageSize" :total="totalusers" />
      </div>
    </div>
    <!-- </el-card> -->



    <!-- 添加模态款 -->
    <el-dialog :title="title" :visible.sync="modelMsg">
      <div class="msgDiv">
        <!-- <h3>店铺基础信息</h3> -->
        <div>
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item v-show="vshow" label="序号">
              <el-input v-model="form.id" disabled></el-input>
            </el-form-item>
            <el-form-item label="类型名称" :required="true">
              <el-input v-model="form.name" placeholder="类型名称"></el-input>
            </el-form-item>
          <el-form-item label="类型简介" :required="true">
            <el-input v-model="form.desc" placeholder="类型简介"></el-input>
          </el-form-item>
            <el-form-item label="排序">
              <el-input v-model="form.sort" placeholder="排序"></el-input>
            </el-form-item>
        <el-form-item label="状态" :required="true" style="text-align: left;line-height: 40px;">
          <el-select v-model="form.status">
            <el-option v-for="opt in options" :key="opt.id" :value="opt.id" :label="opt.label" />
          </el-select>
        </el-form-item>
        </el-form>
        <el-button type="success" @click="addMsg(form)">{{addORupdate}}</el-button>
      </div>
  </div>
  </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        modelMsg: false,
        keyword:"",
        status:"",
        form: {
          id: "",
          name: "",
          desc: "",
          sort: "",
          status: "0",
        },
        options: [{
          id: '0',
          label: "禁用"
        }, {
          id: '1',
          label: "启用"
        }],
        vshow: false,
        loading: false,
        title: "",
        addORupdate: "",
        formdata: [],
        totalusers: 0, //总行数
        pageSize: 5, //每页几行
        startpage: 1, //起始页
      }
    },
    mounted() {
      this.getCourseTypeList()
    },
    methods: {
      handleSubmit(){
          this.getCourseTypeList()
      },
      handleReset(){
        this.keyword=""
        this.status=""
      },
      //获取分页列表
      getCourseTypeList() {
        this.formdata = []
        let params = {
          "page": this.startpage,
          "limit": this.pageSize,
          "keyword":this.keyword,
          "status":this.status
        }
        //调用后台提供的接口方法
        this.$api.getCourseTypeList(params).then(res => {
          console.log(res)
          if (res.data.level == "success") {
            this.formdata = res.data.data
            this.totalusers = res.data.page_info.total
            this.loading = false
          } else {
            this.$message.error(res.data.message)
            this.loading = false
          }

        })
      },
      //分页 pageChange当前页发生改变触发函数
      async pageChange(page) {
        this.startpage = page
        this.loading = true
        //调用获取列表的方法
        this.getCourseTypeList()
      },
      addmodel() {
        this.modelMsg = true
        this.title = '添加课程类型'
        this.addORupdate = "添加",
          this.vshow = false
        this.form.id = "",
          this.form.name = "",
          this.form.desc = "",
          this.form.sort = "",
          this.form.status = "0"
      },
      //提交店铺入驻申请
      async addMsg(form) {
        console.log(form)
        //判断带星号的是否为空

        if ((form.name && form.desc && form.status) == "") {
          this.$message({
            message: "带星号为必填项，请完善信息！",
            type: "warning"
          })
          return false;
        }

        if (form.id > 0) {
          /* 修改 */
          /* 显示加载中。。。 */
          const loadingOne = this.$loading({
            lock: true,
            text: '保存中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          console.log(form)
          await this.$api.setCourseType(form).then(res => {
            console.log(res)
            this.$message({
              type: res.data.level,
              message: res.data.message
            });
            if (res.data.level == "success") {
              this.getCourseTypeList()
              loadingOne.close()
              this.modelMsg = false
            } else {
              this.modelMsg = true
              loadingOne.close()
            }

          })
        } else {
          /* 显示加载中。。。 */
          const loadingOne = this.$loading({
            lock: true,
            text: '保存中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          /* 添加 */
          await this.$api.setCourseType(form).then(res => {
            this.$message({
              type: res.data.level,
              message: res.data.message
            });
            if (res.data.level == "success") {
              this.getCourseTypeList()
              loadingOne.close()
              this.modelMsg = false
            } else {
              loadingOne.close()
              this.modelMsg = true
            }

          })
        }

      },
      //修改弹出框
      async updateMsg(row) {
        console.log(row)
        this.modelMsg = true
        this.title = '修改课程类型'
        this.addORupdate = "修改",
          this.vshow = true
        this.form.id = row.id,
          this.form.name = row.name,
          this.form.desc = row.desc,
          this.form.sort = row.sort,
          this.form.status = row.status

      },
      //删除店铺信息
      async delMsg(id) {
        let delid = {
          id: id
        }
        await this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          const loadingOne = this.$loading({
            lock: true,
            text: '保存中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          this.$api.delCourseType(delid).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            if (res.data.level == "success") {
              loadingOne.close()
              this.getCourseTypeList()
            } else {
              this.$message.error(res.data.message)
              loadingOne.close()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });


      }
    }
  }
</script>
<style scoped="scoped">
  /deep/.el-card__header {
    background-color: #409EFF;
    color: whitesmoke;
  }

  /deep/.el-dialog {
    margin-top: 10vh !important;
    width: 33%;
  }

  .msgDiv {
    width: 80%;
    margin: auto;
    text-align: center;
  }

  .el-input {
    display: flex;
    flex-grow: <2>;
  }
</style>
